<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small">
            <el-form-item label="商家">
              <el-select
                v-model="listPram.shopId"
                clearable
                placeholder="请选择商家"
                @change="shopChange"
              >
                <el-option
                  v-for="item in shopList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关键词：">
              <el-input
                v-model="listPram.keywords"
                placeholder="请输入关键词"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handerSearch"
                  size="small"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button
          size="small"
          type="primary"
          class="mr10"
          v-hasPermi="['admin:article:save']"
          @click="handleAddMenu({ id: 0, name: '顶层目录' })"
          >添加等级</el-button
        >
      </div>
      <el-table
        v-loading="listLoading"
        :data="listData.list"
        size="mini"
        class="table"
        highlight-current-row
        :header-cell-style="{ fontWeight: 'bold' }"
      >
        <el-table-column prop="id" label="ID" min-width="180"></el-table-column>
        <el-table-column
          prop="shopName"
          label="商家名称"
          min-width="180"
        ></el-table-column>
        <el-table-column label="类型" min-width="180">
          <template slot-scope="scope">
            {{ dengjiObj[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="描述"
          min-width="180"
        ></el-table-column>
        <el-table-column prop="experience" label="升级经验值" min-width="180">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? scope.row.experience : "" }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="图片" min-width="80">
            <template slot-scope="scope">
              <div class="demo-image__preview">
                <el-image
                  style="width: 36px; height: 36px"
                  :src="scope.row.img"
                  :preview-src-list="[scope.row.img]"
                />
              </div>
            </template>
          </el-table-column> -->

        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              active-text="显示"
              inactive-text="隐藏"
              :active-value="true"
              :inactive-value="false"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="100"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
               @click="handleEditMenu(scope.row)"
              v-hasPermi="['admin:article:delete']"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handlerDelete(scope.row)"
              v-hasPermi="['admin:article:delete']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :title="editDialogConfig.isCreate === 0 ? '创建等级' : '编辑等级'"
      :visible.sync="editDialogConfig.visible"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <edit
        v-if="editDialogConfig.visible"
        :prent="editDialogConfig.prent"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.data"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>
  
  <script>
import { getDengjiList, delDengji } from "@/api/dengji";
import { dengjiObj } from "@/utils/constants.js";
import { shopListApi } from "@/api/shopPoint";
import edit from "./set";
export default {
  name: "DengjiList",
  components: { edit },
  data() {
    return {
      dengjiObj,
      constants: this.$constants,
      listPram: {
        shopId: null,
        page: 1,
        limit: this.$constants.page.limit[0],
      },
      listData: { list: [], total: 0 },
      listLoading: true,
      shopList: [],

      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建,1=编辑
        prent: {}, // 父级对象
        data: {},
      },
    };
  },
  mounted() {
    this.handlerGetListData(this.listPram);
    this.getShopList();
  },
  methods: {
    handleAddMenu(rowData) {
      this.editDialogConfig.isCreate = 0;
      this.editDialogConfig.prent = rowData;
      this.editDialogConfig.data = {};
      this.editDialogConfig.visible = true;
    },
    handleEditMenu(rowData) {
      this.editDialogConfig.isCreate = 1;
      this.editDialogConfig.data = rowData;
      this.editDialogConfig.prent = rowData;
      this.editDialogConfig.visible = true;
    },
    getShopList() {
      shopListApi({
        page: 1,
        limit: 100,
        status: 1,
        temp: new Date().getTime(),
      }).then((res) => {
        this.shopList = res.list;
      });
    },
    shopChange() {
      this.listPram.page = 1;
      this.handlerGetListData(this.listPram);
    },
    handerSearch() {
      this.listPram.page = 1;
      this.handlerGetListData(this.listPram);
    },
    handlerGetListData(pram) {
      this.listLoading = true;
      getDengjiList(pram).then((data) => {
        this.listData.list = data;
        this.listData.total = data.length;
        this.listLoading = false;
      });
    },
    // handlerOpenEdit(isEdit, editData) {
    //   // 0=add 1=edit
    //   this.$router.push({
    //     name: "DengjiSet",
    //     query: {
    //       edit: isEdit,
    //       id: editData ? editData.id : "",
    //     },
    //   });
    // },
    handlerHideDialog() {
      this.handlerGetListData(this.listPram);
      this.editDialogConfig.visible = false;
    },
    handlerDelete(rowData) {
      this.$confirm("确定删除当前数据", "提示").then((result) => {
        delDengji(rowData.id).then((data) => {
          this.$message.success("删除数据成功");
          this.handlerGetListData(this.listPram);
        });
      });
    },
    handleSizeChange(val) {
      this.listPram.limit = val;
      this.handlerGetListData(this.listPram);
    },
    handleCurrentChange(val) {
      this.listPram.page = val;
      this.handlerGetListData(this.listPram);
    },
    hideEditDialog() {
      setTimeout(() => {
        this.editDialogConfig.prent = {}
        this.editDialogConfig.type = 0
        this.editDialogConfig.visible = false
        this.handlerGetListData(this.listPram);
      }, 200)
    },
  },
};
</script>
  
  <style scoped lang="scss">
.articleModal {
  z-index: 333 !important;
}
.flex {
  display: flex;
  align-items: center;
  &-1 {
    flex: 1;
  }
  &-2 {
    flex: 2;
  }
}
</style>
  